package com.jeff.utils;

import android.text.TextUtils;

/**
 * author：duff
 * version：1.0.0
 * date：2017/8/31
 */
public class NumberUtil {

    public static int toInt(String s) {
        try {
            return Integer.parseInt(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static long toLong(String s) {
        try {
            return Long.parseLong(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0L;
    }

    public static float toFloat(String s) {
        try {
            return Float.parseFloat(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0.0f;
    }

    public static boolean toBoolean(String s) {
        try {
            return Boolean.parseBoolean(s);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    public static String toString(int i) {
        return String.valueOf(i);
    }

    public static String toString(float f) {
        return String.valueOf(f);
    }

    public static String toString(boolean b) {
        return String.valueOf(b);
    }

    public static String toString(long l) {
        return String.valueOf(l);
    }

    public static String formatPhoneNumber(String phoneNumber) {
        if (!TextUtils.isEmpty(phoneNumber)) {
            StringBuilder validPhoneNumber = new StringBuilder();
            char[] phoneNumChar = phoneNumber.toCharArray();
            for (char c : phoneNumChar) {
                if (c >= '0' && c <= '9') {
                    validPhoneNumber.append(c);
                }
            }

            return validPhoneNumber.toString();
        }

        return phoneNumber;
    }

}
